package org.szt.humwiki.page;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.szt.humwiki.page.service.PageService;

@Controller
public class PageController {
	@Autowired
	private PageService pageService;

	@RequestMapping("/page/{name}")
	public String page(@PathVariable String name, Model model)
			throws UnsupportedEncodingException {
		WikiPage page = pageService.getPageByName(name);
		if (page == null) {
			model.addAttribute("haspage", "false");
		} else {
			model.addAttribute("haspage", "true");
		}
		String nameDecodelater = new String(name.getBytes("iso-8859-1"),
				"utf-8");
		model.addAttribute("name", nameDecodelater);
		return "page";
	}

	@RequestMapping("/page/{name}/edit")
	public String edit(@PathVariable String name, Model model)
			throws UnsupportedEncodingException {
		WikiPage page = pageService.getPageByName(name);
		String content = "";
		if (page != null) {
			content=page.getContent();
		}
		String nameDecodelater = new String(name.getBytes("iso-8859-1"),
		"utf-8");
		model.addAttribute("name", nameDecodelater);
		model.addAttribute("content", content);
		return "edit";
	}

	@RequestMapping("/page/{name}/post")
	public String post(@PathVariable String name, Model model) {
		WikiPage page = pageService.getPageByName(name);
		String content = "";
		if (page != null) {

		}
		try {
			model.addAttribute("name", java.net.URLDecoder
					.decode(name, "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		model.addAttribute("content", "111111");
		return "edit";
	}

	public PageService getPageService() {
		return pageService;
	}

	public void setPageService(PageService pageService) {
		this.pageService = pageService;
	}

}
